package cn.tedu.v3_1;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * Server 端---流置空  升级  JavaOOP 私有化属性
 *
 */
public class Server {
    private ServerSocket serverSocket;
    private String ip;//String ip=null;  申请端口----没有IP
    private static final Integer PORT=0423;
    public Server() {
        try {
            serverSocket = new ServerSocket(PORT);
        } catch (BindException e) {
            System.out.println("端口号已经被占用,请检查端口号");
        } catch (IOException e) {
            System.out.println("客户端主动退出了");
        }
    }

    public void start() {

        while (true) {
            try {
                System.out.println("等待客户端连接");
             Socket socket = serverSocket.accept();

                new Thread(new CS(socket)).start();

            } catch (IOException e) {
                System.out.println(ip+"客户端链接失效了~");
            } finally {

               if(serverSocket!=null&&serverSocket.isClosed()){
                   try {
                       serverSocket.close();
                   } catch (IOException e) {
                       System.out.println("服务器关闭失败~~~~~");
                   }
                   System.out.println("服务器关闭成功~~~~~Over");
               }
            }
        }

    }

    public static void main(String[] args){
        Server server = new Server();
        server.start();
    }

    private class CS implements Runnable {
        private Socket socket;
        private  String ip;

        private PrintWriter pw;
        private BufferedReader br;

        public CS(Socket s){
            this.socket=s;
            ip=socket.getInetAddress().getHostAddress();
            try {
                br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
                pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8")),true);
            } catch (IOException e) {
                System.out.println("客户端断开链接了~");
            }
        }



        public void run() {
            //输入流--读入功能：存入线程中

          try {
              new Thread(() -> {
                  try {
                      String message;
                      while ((message=br.readLine())!=null) {
                          System.out.println(ip + "客户端端说：" + message);//打印  客户端发过来文本
                          System.out.println("服务器请输入：");
                      }
                  } catch (IOException e) {
                      System.out.println("客户端发来的消息断开了~");
                  }
              }).start();

              //客户端一直跟服务器端----说【循环】
              System.out.println("服务器端用户：请输入文字：");
              Scanner input = new Scanner(System.in);//测试---控制台输入文本
              while (true) {//死循环---一直与服务器说话
                  String say = input.nextLine();
                  pw.println("服务器端说：" + say);        //写出  一行行文字
                  System.out.println("写出完成嘞~");

              }
                  } finally {
                  if(pw!=null)
                    pw.close();
                  if (br!=null)
                      try {
                          br.close();
                      } catch (IOException e) {
                          System.out.println("读入流断开链接~");
                      }
              if (socket != null && !socket.isClosed()){
                       System.out.println(ip + "客户端关闭成功！！！success！");
                             try {
                                   socket.close();
                             } catch (IOException e) {
                              System.out.println(ip + "一个客户端断开链接了~~~");
                          }
                      }


                  }
              }
          }

                }


